﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace Lilium.LParse.Environment
{
    public static class CommandLineParsers
    {
        static CommandLineParsers()
        {
            QuotedString =
                TextParsers.Not('"').Many1()
                    .Between(
                        TextParsers.Char('"'),
                        TextParsers.Char('"'));

            WhiteSpace = ParserCombinators.Choise(new[] {
                TextParsers.Category(UnicodeCategory.SpaceSeparator),   // Any character with Unicode class Zs
                TextParsers.Char('\u0009'),                             // Horizontal tab character (U+0009)
                TextParsers.Char('\u000B'),                             // Vertical tab character (U+000B)
                TextParsers.Char('\u000C'),                             // Form feed character (U+000C)
            });
        }

        public static Parser<char, Tuple<string, TArguments>> CommandLine<TArguments>(Parser<char, TArguments> argumentsParser)
        {
            return
                from _1 in WhiteSpace.SkipMany()
                from command in QuotedString.Try()
                    .Or(TextParsers.Many1Till(WhiteSpace.OrEof()))
                from _2 in WhiteSpace.SkipMany()
                from arguments in argumentsParser
                select Tuple.Create(command, arguments);
        }

        public static readonly Parser<char, string> QuotedString;
        public static readonly Parser<char, char> WhiteSpace;
    }
}
